<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Notepad</title>

    <!-- bootstrap -->
    <link rel="stylesheet" href="./assets/css/bootstrap.min.css" />

    <!-- MUI -->
    <link rel="stylesheet" href="./assets/plugins/mui/css/mui.min.css" />

    <!-- animate.css -->
    <link rel="stylesheet" href="./assets/css/animate.min.css" />

    <!-- 引入bootstrapValidator表单验证插件 -->
    <link rel="stylesheet" href="./assets/plugins/bootstrap-validator/bootstrapValidator.min.css">

    <!-- 城市插件 -->
    <link rel="stylesheet" href="./assets/plugins/city-picker/city-picker.css" />

    <!-- 重置样式 -->
    <link rel="stylesheet" href="./assets/css/reset.css" />

    <!-- 公共样式 -->
    <link rel="stylesheet" href="./assets/css/common.css" />

    <!-- 页面独立的 -->
    <link rel="stylesheet" href="./assets/css/ContactAdd.css" />

    <!-- jquery  -->
    <script src="./assets/js/jquery.min.js"></script>

    <!-- 引入bootstrapValidator表单验证插件 -->
    <script src="./assets/plugins/bootstrap-validator/bootstrapValidator.min.js"></script>
</head>
<body>
    <!-- 信息录入 -->
    <div class="info">
        <h3 class="title wow fadeInUp">信息录入</h3>

        <p class="desc wow fadeInUp" data-wow-delay="200ms">
            您可在本页录入您的联系信息，录入成功后名片将会出现在首页。如需修改首页名片信息，也请新建录入，将会自动替换原有名片。
        </p>
    </div>

    <form id="contact" class="contact wow fadeInUp" data-wow-delay="300ms">
        <div class="form-group">
            <label for="nickname">姓名</label>
            <input type="text" class="form-control" name="nickname" id="nickname" placeholder="请输入姓名" required />
        </div>

        <div class="form-group">
            <label for="mobile">手机</label>
            <input type="text" class="form-control" name="mobile" id="mobile" placeholder="请输入手机" required />
        </div>

        <div class="form-group">
            <label for="gender">性别</label>
            <select id="gender" name="gender" class="form-control" required>
                <option value="0">保密</option>
                <option value="1">男</option>
                <option value="2">女</option>
            </select>
        </div>

        <div class="form-group">
            <label for="type">分类</label>
            <select id="typeid" name="typeid" class="form-control" required></select>
        </div>

        <div class="form-group" style="position: relative;">
            <label for="region">城市</label>
            <input id="region" type="text" name="region" readonly placeholder="请选择城市" />
        </div>

        <div class="form-group">
            <label for="remark">备注</label>
            <input type="text" class="form-control" name="remark" id="remark" placeholder="请输入备注" />
        </div>
        
        <div class="action">
            <button type="submit" name="submit" class="btn">提交</button>
        </div>
    </form>
</body>
</html>
<!-- wow.js -->
<script src="./assets/js/wow.min.js"></script>

<!-- bootstrap -->
<script src="./assets/js/bootstrap.min.js"></script>

<!-- MUI -->
<script src="./assets/plugins/mui/js/mui.min.js"></script>

<!-- axios请求 -->
<script src="./assets/plugins/axios/axios.min.js"></script>
<script src="./assets/plugins/axios/request.js"></script>

<!-- cookie -->
<script src="./assets/plugins/cookie.js"></script>

<!-- 判断是否登录 -->
<script src="./assets/plugins/login.js"></script>

<!-- 城市插件 -->
<script src="./assets/plugins/city-picker/city-picker.data.min.js"></script>
<script src="./assets/plugins/city-picker/city-picker.min.js"></script>

<script>
    //实例化wow.js
    new WOW().init()

    //初始化赋值
	$("#region").citypicker();

    //获取当前登录信息
    var business = cookie.get('business') ? JSON.parse(cookie.get('business')) : {};

    //获取通讯录分类数据
    (async () => {
        var result = await POST({url:'/card/card/type'})
        if(result.code == 1 && result.data.length > 0)
        {
            for(var item of result.data)
            {
                var option = `<option value='${item.id}'>${item.name}</option>`
                $(`select[name=typeid]`).append(option)
            }
        }
    })()

    //表单验证
    $('#contact').bootstrapValidator({
        // 所有字段的默认错误消息
        message: '表单内容不能为空',
        //字段校验图标。默认bootstrap图标
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        // 验证字段
        fields: {
            mobile: {
                message: '手机号码验证失败',
                validators: {
                    notEmpty: {
                        message: '手机号码不能为空'
                    },
                    stringLength: {
                        min: 11,
                        max: 11,
                        message: '手机号码长度为11位'
                    },
                    regexp: {
                        regexp: /^1[3,4,5,6,7,8,9][0-9]{9}$/,
                        message: '手机号码格式有误'
                    }
                }
            },
            nickname: {
                validators: {
                    message: '昵称验证失败',
                    notEmpty: {
                        message: '请输入昵称'
                    },
                }
            },
            typeid: {
                validators: {
                    message: '分类验证失败',
                    notEmpty: {
                        message: '请选择分类'
                    },
                }
            },
        }
    }).on('success.form.bv', function(e){
        //阻止多次提交
        e.preventDefault()
    })

    $('#contact').on('submit', async function(e){
        e.preventDefault()

        //通过表单验证后再提交
        if(!$("form").data('bootstrapValidator').isValid())
        {
            //重置表单
            $('form').data('bootstrapValidator').resetForm();
            return false
        }

        //组装数据
        var data = {
            businessid: business.id,
            mobile: $.trim($('#mobile').val()),
            nickname: $.trim($('#nickname').val()),
            gender: $.trim($('#gender').val()),
            remark: $.trim($('#remark').val()),
            typeid: $.trim($('#typeid').val())
        }

        var citypicker = $('#region').data('citypicker')
        var region = citypicker.getCode("district") || citypicker.getCode("city") || citypicker.getCode("province")

        if(region)
        {
            data.region = region
        }

        var result = await POST({
            url: '/card/card/add',
            params: data
        })

        if(result.code == 1)
        {
            mui.toast(result.msg, {duration: 1000})

            setTimeout(() => {
                location.href = 'index.html'
            }, 500)
        }else
        {
            //重置表单
            $('form').data('bootstrapValidator').resetForm()
            mui.toast(result.msg)
            return false
        }
    })
</script>